package com.cyt.chattlibrary.message;

import android.os.Parcel;
import android.os.Parcelable;

import com.cyt.chattlibrary.message.model.MessageContent;

/**
 * @author QiQi
 * @ClassName: Message
 * @date 2018/10/20
 * @description
 */
public class Message implements Parcelable {
    public static final Creator<Message> CREATOR = new Creator<Message>() {
        @Override
        public Message createFromParcel(Parcel source) {
            return new Message(source);
        }

        @Override
        public Message[] newArray(int size) {
            return new Message[size];
        }
    };
    /**
     * 发送目标
     */
    private String targetId;
    /**
     * 消息Id
     */
    private int messageId;
    /**
     * 消息的发送方向，是发送出去的还是接收进来的
     */
    private MessageDirection messageDirection;
    /**
     * 发送者ID
     */
    private String senderUserId;
    /**
     * 接收时间
     */
    private long receivedTime;
    /**
     * 发送时间
     */
    private long sendTime;
    /**
     * 消息类型
     */
    private MessageContentType type;
    private String objectName;
    /**
     * 发送消息体
     */
    private MessageContent content;

    public Message() {
    }

    protected Message(Parcel in) {
        this.targetId = in.readString();
        this.messageId = in.readInt();
        this.messageDirection = MessageDirection.setValue(in.readInt());
        this.senderUserId = in.readString();
        this.receivedTime = in.readLong();
        this.sendTime = in.readLong();
        this.type = MessageContentType.setValue(in.readInt());
        this.objectName = in.readString();
        this.content = in.readParcelable(MessageContent.class.getClassLoader());
    }

    public static Message obtain(String targetId, MessageContent content) {
        Message message = new Message();
        message.setTargetId(targetId);
        if (content instanceof TextMessage) {
            message.setType(MessageContentType.TEXT_MESSAGE);
        } else if (content instanceof ImageMessage) {
            message.setType(MessageContentType.IMAGE_MESSAGE);
        } else if (content instanceof VoiceMessage) {
            message.setType(MessageContentType.VOICE_MESSAGE);
        }
        message.setMessageDirection(MessageDirection.SEND);
        message.setSendTime(System.currentTimeMillis());
        message.setContent(content);
        return message;
    }

    public String getTargetId() {
        return targetId == null ? "" : targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public int getMessageId() {
        return messageId;
    }

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }

    public MessageDirection getMessageDirection() {
        return messageDirection;
    }

    public void setMessageDirection(MessageDirection messageDirection) {
        this.messageDirection = messageDirection;
    }

    public String getSenderUserId() {
        return senderUserId == null ? "" : senderUserId;
    }

    public void setSenderUserId(String senderUserId) {
        this.senderUserId = senderUserId;
    }

    public long getReceivedTime() {
        return receivedTime;
    }

    public void setReceivedTime(long receivedTime) {
        this.receivedTime = receivedTime;
    }

    public long getSendTime() {
        return sendTime;
    }

    public void setSendTime(long sendTime) {
        this.sendTime = sendTime;
    }

    public String getObjectName() {
        return objectName == null ? "" : objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public MessageContent getContent() {
        return content;
    }

    public void setContent(MessageContent content) {
        this.content = content;
    }

    public MessageContentType getType() {
        return type;
    }

    public void setType(MessageContentType type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof Message) {
            return this.messageId == ((Message) obj).messageId;
        } else {
            return super.equals(obj);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.targetId);
        dest.writeInt(this.messageId);
        dest.writeInt(this.messageDirection == null ? -1 : this.messageDirection.ordinal());
        dest.writeString(this.senderUserId);
        dest.writeLong(this.receivedTime);
        dest.writeLong(this.sendTime);
        dest.writeInt(this.type == null ? -1 : this.type.ordinal());
        dest.writeString(this.objectName);
        dest.writeParcelable(this.content, flags);
    }

    public static enum MessageDirection {
        SEND(1),
        RECEIVE(2);

        private int value = 1;

        MessageDirection(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Message.MessageDirection setValue(int code) {
            Message.MessageDirection[] arr = values();

            for (MessageDirection c : arr) {
                if (code == c.getValue()) {
                    return c;
                }
            }
            return SEND;
        }
    }

    public static enum MessageContentType {
        TEXT_MESSAGE(1),
        IMAGE_MESSAGE(2),
        VOICE_MESSAGE(3);

        private int value = 1;

        MessageContentType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static MessageContentType setValue(int code) {
            MessageContentType[] arr = values();
            for (MessageContentType c : arr) {
                if (code == c.getValue()) {
                    return c;
                }
            }
            return TEXT_MESSAGE;
        }
    }
}
